<?php

namespace Home\Controller;
use Wechat\JsSdk;
use Wechat\Wechat;

class SnsApiController extends HomeController
{

    public function subscribe($openid='')
    {
        if (!empty($openid)) {
            cookie('openid', $openid, time()+24*3600*30);
            $sceneId = cookie("scene_id");
            if (!empty($sceneId)) {
                $addFan = D("AddFanLog")->where(['scene_id'=>$sceneId])->find(['order'=>"id desc"]);
                $addFan['openid'] = $openid;
                D("AddFanLog")->where(['id'=>$addFan['id']])->save($addFan);
            }
        }
        redirect("http://mp.weixin.qq.com/s?__biz=MzI2NzQwNzY5NQ==&mid=2247484029&idx=1&sn=4156bdea27dc62c8cd07563c2cc613c9&chksm=eafe1a5ddd89934ba3d2afd3d83575e3d7a75343987f883946b93830ba1a12c71d0b4678409e&scene=0#wechat_redirect");
    }

	public function getOpenid(){
        $token = 'gh_2de5c0c2d52e';
        $appInfo = get_token_appinfo ( $token );
        $option = [
            'appid'=>$appInfo['appid'],
            'appsecret'=>$appInfo['secret'],
            'encodingAesKey'=>$appInfo['encodingaeskey'],
            'token'=>$token
        ];

        $redirectUri = 'http://'.getenv("HTTP_HOST")."?".getenv("QUERY_STRING");
        $wechat = new Wechat($option);
        $getCodeUrl = $wechat->getOauthRedirect($redirectUri, '', 'snsapi_base');
        if (!isset($_GET['code']) || empty($_GET['code'])) {
            redirect($getCodeUrl);
        }
        $user = $wechat->getOauthAccessToken();
        if (isset($user['openid'])) {
            cookie('weixin_openid', $user['openid']);
        }
        $redirectUrl = I("get.redirect_url");
        if (!empty($redirectUrl)){
            redirect($redirectUrl);
        }
        redirect("http://touch.mescake.com");
    }
}


?>